package net.wanho.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.po.Account;
import net.wanho.service.UserService;
import net.wanho.service.impl.UserServiceImpl;
import net.wanho.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * Author：汤小洋
 * Date：2025-09-15 10:56
 * Description：<描述>
 */
// 默认是预实例化，配置为懒初始化
@Lazy
// @Controller
// @ResponseBody
@RestController
// @RequiredArgsConstructor
@RequestMapping("/user")
@Api(tags = "01.用户模块")
public class UserController {

    // 方式1：基于field注入
    // @Autowired
    // @Resource
    private final UserService userService;

    // 方式2：基于setter方法注入
    // @Autowired
    // public void setUserService(UserService userService) {
    //     this.userService = userService;
    // }

    // 方式3：基于constructor注入（优点：可以为final属性注入、保证属性不为null、确保属性完成初始化），Spring官方推荐
    // @Autowired
    public UserController(UserService userService) {
        // System.out.println("实例化UserController......................");
        this.userService = userService;
    }

    @Value("${address}")
    private String address;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AjaxResult login(@ApiParam("用户名") String username, @ApiParam("密码") String password){
        userService.login();
        Account account = new Account(1, username, password, "", null, LocalDateTime.now());
        return AjaxResult.success(200, "登录成功", account);
    }

    // 初始化方法，执行初始化操作
    @PostConstruct
    public void init(){
        this.address = this.address.toUpperCase();
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public String register(@ApiParam("账号信息") @RequestBody Account account){
        userService.register(account);
        return "注册成功";
    }
}
